package com.cloud.core.graphx.demo

import org.apache.spark.graphx.{Edge, Graph, VertexId}
import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object Graph_learning_04 {

  var sc: SparkContext = null
  val master = "local"
  val appName = "Graph_learning_04"

  // 初始化SparkContext
  def init(): Unit = {

    val sparkConf = new SparkConf()
      .setMaster(master)
      .setAppName(appName)

    sc = new SparkContext(sparkConf)
  }

  def main(args: Array[String]): Unit = {

    init()

    // Create an RDD for the vertices
    val users: RDD[(VertexId, (String, String))] =
      sc.parallelize(Seq((3L, ("rxin", "student")),
        (7L, ("jgonzal", "postdoc")),
        (5L, ("franklin", "prof")),
        (2L, ("istoica", "prof")),
        (4L, ("peter", "student"))))
    // Create an RDD for edges
    val relationships: RDD[Edge[String]] =
      sc.parallelize(Seq(Edge(3L, 7L, "collab"),
        Edge(5L, 3L, "advisor"),
        Edge(2L, 5L, "colleague"),
        Edge(5L, 7L, "pi"),
        Edge(4L, 0L, "student"),
        Edge(5L, 0L, "colleague")))

    // Define a default user in case there are relationship with missing user
    val defaultUser = ("John Doe", "Missing")
    // Build the initial Graph
    val graph = Graph(users, relationships, defaultUser)

    def max(a: (VertexId, Int), b: (VertexId, Int)): (VertexId, Int) = {
      if (a._2 > b._2) a else b
    }

    val maxInDegree = graph.inDegrees.reduce(max)
    val maxOutDegree = graph.outDegrees.reduce(max)
    val maxDegree = graph.degrees.reduce(max)
    println(maxInDegree)
    println(maxOutDegree)
    println(maxDegree)
  }
}
